from django import forms
from web import models
from web.forms.widgets import ColorRadioSelect


class ProjectModelForm(forms.ModelForm):
    class Meta:
        model = models.Project
        fields = ['name', 'color', 'desc', 'star']
        widgets = {
            'desc': forms.Textarea(attrs={'rows': 3}),
            'color': ColorRadioSelect(),
            'star': forms.CheckboxInput(attrs={'class': 'star-checkbox'}),
        }
        labels = {
            'name': '项目名称',
            'color': '项目颜色',
            'desc': '项目描述',
            'star': '设为星标',
        }
        error_messages = {
            'name': {
                'required': '项目名称不能为空',
                'max_length': '项目名称不能超过32个字符',
            },
            'color': {
                'required': '请选择项目颜色',
            },
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for name, field in self.fields.items():
            if name != 'star':
                field.widget.attrs['class'] = 'form-control'